package cn.com.dhcc.fornew.pet.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.dhcc.app.core.base.BaseService;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.component.pager.Pager;
import cn.com.dhcc.app.core.component.pager.PagerData;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.pub.core.CM;
import cn.com.dhcc.fornew.pet.dao.DogDao;
import cn.com.dhcc.fornew.pet.vo.Dog;

/**
 * 
 * @日期：2017-6-1下午6:56:09
 * @作者： 杨贵隆
 */

@Service
public class DogService extends BaseService{
	
	@Autowired
	private DogDao dao;
	/**
	 * 列表
	 * @param pager
	 * @param condition
	 * @return
	 * @throws ServiceException
	 */
	public PagerData<Dog> list(Pager pager, WhereCondition condition) throws ServiceException {
		try {
			return dao.list(pager, condition);
		} catch (DaoException e) {
			throw new ServiceException("获取分页信息列表失败", e);
		}
	}
	/**
	 * 增加
	 * @param vo
	 * @return
	 * @throws ServiceException
	 */
	public int insert(Dog vo) throws ServiceException {

		try {
			return dao.insert(vo);
		} catch (DaoException e) {
			throw new ServiceException("新增失败", e);
		}
	}
	/**
	 * 更新
	 * @param vo
	 * @return
	 * @throws ServiceException
	 */
	public int update(Dog vo) throws ServiceException {

		try {
			return dao.update(vo);
		} catch (DaoException e) {
			throw new ServiceException("更新失败", e);
		}
	}

	/**
	 * 查看
	 * @param id
	 * @return
	 * @throws ServiceException
	 */
	public Dog get(String id) throws ServiceException {
		try {
		  return dao.get(id);
		} catch (DaoException e) {
			throw new ServiceException("根据id获取失败", e);
		}
		
	}
	/**
	 * 根据ID删除
	 * @param id
	 * @return
	 */
	public boolean delete(String id) {
		boolean result = false;
		String sql = "delete from " + WSD_TABLE("t_dog") 
				+ " WHERE id=?";
		try {
			result = CM.getDao().delete("删除", sql, new Object[]{id})> 0 ? true : false;
		} catch (DaoException e) {
			new ServiceException("删除失败", e);
		}
		return result;
	}
	
	
	public boolean isPropertyUnique(String propertyName, Object newValue, Object oldValue) throws ServiceException {
		try {
			if (newValue.equals(oldValue)) {
				return true;
			}
			WhereCondition searchParameter = new WhereCondition();
			searchParameter.where().eq(propertyName, newValue);
			return getDao().countModule(null,null,searchParameter) == 0;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		}
	
}
	
	
	

